#!/bin/bash

#Данные из скрипта namePath
NAMEPROG=$1
DIRINST=$2
DIRPROG=$3
DIRREPPROG=$4
VERSION=$5
NAMEPK=$6
NAMEPKG=$7
NAMEPROGRU=$8
COMMENTRU=$9

DIRROOT=$(cd $(dirname $0)&& pwd)
TMPDIR=$DIRROOT/.tmp_deb
touch -f md5sums_$NAMEPROG
chmod 0644 md5sums_$NAMEPROG

mkdir -p $TMPDIR
cd $TMPDIR


mkdir -p ./DEBIAN/
mkdir -p .$DIRINST$DIRPROG
mkdir -p ./usr/share/pixmaps
mkdir -p ./usr/share/applications/
mkdir -p ./usr/share/fly-wm/Desktops/Desktop1/
mkdir -p ./usr/share/doc/$NAMEPROG/

cd $TMPDIR/usr/share/doc/$NAMEPROG/
touch -f copyright
chmod 0644 copyright
{
echo "Copyright"
} > $TMPDIR/usr/share/doc/$NAMEPROG/copyright


cd $TMPDIR/DEBIAN/

touch -f control
chmod 0644 control
{
echo "Package: $NAMEPKG
Version: $VERSION
Provides: $NAMEPK
Maintainer: prog <src@makeyev.ru>
Architecture: amd64
Priority: optional
Section: other
Depends: libsqlite3-0 (>=3.36.0-astra1)
Description: Install the program $NAMEPROG"
} > $TMPDIR/DEBIAN/control

touch -f md5sums
chmod 0644 md5sums

touch -f dirs
chmod 0644 dirs
{
echo "$DIRINST$DIRPROG/
$DIRINST/source/
/usr/share/pixmaps
/usr/share/doc
/usr/share/applications/
/usr/share/fly-wm/Desktops/Desktop1/"
} > $TMPDIR/DEBIAN/dirs

cmd=$DIRINST$DIRPROG/$NAMEPROG
icon=/usr/share/pixmaps/$NAMEPROG.png

cd $TMPDIR/usr/share/applications/
touch -f $NAMEPROG.desktop
chmod 0644 $NAMEPROG.desktop
{
echo "[Desktop Entry]
Encoding=UTF-8
Name=$NAMEPROG
Name[ru]=$NAMEPROGRU
Comment[ru]=$COMMENTRU
Type=Application
NoDisplay=false
Exec=$cmd
Icon=application-x-executable
Icon[ru]=$icon
Comment=$NAMEPROG
Comment[ru]=$COMMENTRU
X-FLY-IconContext=MimeTypes
Hidden=false
Terminal=false
StartupNotify=false
URL=$cmd"
} > $NAMEPROG.desktop

cd $TMPDIR$DIRINST$DIRPROG/
NAMEUNI=$NAMEPROG"_uninstall.desktop"
touch -f $NAMEUNI
chmod 0775 $NAMEUNI 
{
echo "[Desktop Entry]
Encoding=UTF-8
Name=$NAMEPKG uninstall
Name[ru]=$NAMEPKG удаление
Type=Application
NoDisplay=false
Exec=sudo apt-get remove ${NAMEPKG,,}
Icon=terminal
X-FLY-IconContext=MimeTypes
Hidden=false
Terminal=true
StartupNotify=false"
} > $NAMEUNI

cd $TMPDIR/DEBIAN/

touch -f prerm
chmod 0775 prerm
{
echo "#!/bin/bash
"
} > $TMPDIR/DEBIAN/prerm

cd $TMPDIR/DEBIAN/
touch -f postinst
chmod 0775 postinst
#Копируется из for_dpkg_deb-lib.sh команда для создания ссылки на собственную библиотеку????
{
echo "#!/bin/bash
sudo chmod -R 0775 $DIRINST
sudo chmod 444 /usr/share/pixmaps/$NAMEPROG.png
sudo chmod 444 /usr/share/applications/$NAMEPROG.desktop
sudo chmod 444 /usr/share/fly-wm/Desktops/Desktop1/$NAMEPROG.desktop
sudo chmod 444 /usr/share/doc/$NAMEPROG/copyright"
} > postinst

echo dirroot/dirrepprog:  $DIRROOT/$DIRREPPROG
#sudo sh  $DIRROOT/$DIRREPPROG/make.sh $NAMEPROG
echo 1
cp -n $DIRROOT/$DIRREPPROG/bin/release/$NAMEPROG $TMPDIR$DIRINST$DIRPROG/
echo 2
cp -n $DIRROOT/$DIRREPPROG/resources/png/$NAMEPROG.png $TMPDIR/usr/share/pixmaps/
echo 3
cp -n -r $DIRROOT/$DIRREPPROG/resources/resPROG/* $TMPDIR$DIRINST/
echo 4
cp -n $TMPDIR/usr/share/applications/$NAMEPROG.desktop $TMPDIR/usr/share/fly-wm/Desktops/Desktop1/

#chmod -R 0775 $TMPDIR/*

#cd $TMPDIR/DEBIAN/
#find $TMPDIR/usr -type f -print0|xargs -0 md5sum > md5sums
cd $TMPDIR/usr/
find -type f -print0|xargs -0 md5sum > $TMPDIR/DEBIAN/md5sums

find -type f -print0|xargs -0 md5sum > $DIRROOT/md5sums_$NAMEPROG

cd $DIRROOT/

#rm -r $TMPDIR

